package com.filter;

import com.bean.Admin;
import com.bean.Teacher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "AdminFilter", urlPatterns = "/tgls/admin/*")
public class AdminFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        Object user = request.getSession().getAttribute("user");
        if (user != null) {
            try {
                user = (Admin) user;
                chain.doFilter(req, resp);
            } catch (Exception e) {
                System.out.println("权限错误，禁止访问管理员模块");
            }
        }
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
